package com.mall.market.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.market.mapper.SmsHomeAdvertiseMapper;
import com.mall.model.domain.SmsHomeAdvertise;
import com.mall.model.dto.SmsHomeAdvertiseDto;
import com.mall.model.vo.CommonPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SmsHomeAdvertiseService {
    @Autowired(required = false)
    private SmsHomeAdvertiseMapper smsHomeAdvertiseMapper;

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    public CommonPage list(SmsHomeAdvertiseDto dto) {
        Page<SmsHomeAdvertise> pageInfo = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<SmsHomeAdvertise> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(dto.getName() != null, SmsHomeAdvertise::getName, dto.getName());
        queryWrapper.eq(dto.getType() != null, SmsHomeAdvertise::getType, dto.getType());
        queryWrapper.like(dto.getEndTime() != null, SmsHomeAdvertise::getEndTime, dto.getEndTime());
        Integer count = smsHomeAdvertiseMapper.selectCount(queryWrapper);
        smsHomeAdvertiseMapper.selectPage(pageInfo, queryWrapper);
        return new CommonPage(dto.getPageNum(), dto.getPageSize(), count, pageInfo.getRecords());
    }

    //增
    public void create(SmsHomeAdvertise smsHomeAdvertise) {
        smsHomeAdvertiseMapper.insert(smsHomeAdvertise);
    }

    //删
    public void delete(List ids) {
        smsHomeAdvertiseMapper.deleteBatchIds(ids);
    }

    //修改
//    public void updateStatus(Integer id, Integer recommendStatus) {
//        LambdaQueryWrapper<SmsHomeAdvertise> wrapper = new LambdaQueryWrapper<>();
//        wrapper.in(SmsHomeAdvertise::getId, id);
//      SmsHomeAdvertise smsHomeAdvertises = smsHomeAdvertiseMapper.select(wrapper);
//            if (smsHomeAdvertises != null) {
//                smsHomeAdvertises.setStatus(recommendStatus);
//                smsHomeAdvertiseMapper.updateById(smsHomeAdvertises);
//        }
//    }

    //修改
    public void updateStatus(Integer id, Integer recommendStatus) {
        SmsHomeAdvertise smsHomeAdvertise = smsHomeAdvertiseMapper.selectById(id);
        if (smsHomeAdvertise != null) {
            smsHomeAdvertise.setStatus(recommendStatus);
            smsHomeAdvertiseMapper.updateById(smsHomeAdvertise);
        }
    }

    public void update(SmsHomeAdvertise smsHomeAdvertise) {
        smsHomeAdvertiseMapper.updateById(smsHomeAdvertise);
    }

    /**
     * 获取广告详情
     *
     * @param id
     * @return
     */
    public SmsHomeAdvertise getAdvertiseInfo(Integer id) {
        SmsHomeAdvertise smsHomeAdvertise = smsHomeAdvertiseMapper.selectById(id);
        return smsHomeAdvertise;
    }
}
